<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\game\Gamechild;
use app\common\model\game\Gameroom;


/**
 * 游戏模块
 */
class Game extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    //获取游戏列表
    public function get_game_list()
    {
        $game_model = new Gamechild();
        $game_list = $game_model->order('weigh desc')->select();
        if(isset($game_list) && !empty($game_list)){
            foreach($game_list as $key=>$item){
                $game_list[$key]['game_image'] = cdnurl($item['image'],true);
            }
        }


        $this->success('请求成功',$game_list);
    }


    //创建房间
    public function create_room(){
        $game_ids = $this->request->post('game_ids');
        $pwd = $this->request->post('pwd','');
        $room_type = $this->request->post('room_type');


        if($room_type == 'sfzb'){
            $Gameroom = new Gameroom();
            //先检查之前创建的是否有过期的
            $Gameroom->where('join_user_ids','')->where('user_id',$this->auth->id)->delete();

            $insert['user_id'] = $this->auth->id;
            $insert['pwd'] = $pwd!=''?md5($pwd):'';
            $insert['game_child_ids'] = $game_ids;

            $effect = $Gameroom->save($insert);
            $this->success('创建成功');
        }
    }

    //获取创建的房间
    public function get_room_list(){
        $Gameroom = new Gameroom();
        $room_list = $Gameroom->order('id desc')->select();

        if(isset($room_list) && !empty($room_list)){
            foreach($room_list as $key=>$item){
                if($item['join_user_ids']){
                    if(count(explode(',',$item['join_user_ids'])) == 4){
                        $room_list[$key]['is_full'] = true;
                    }else{
                        $room_list[$key]['is_full'] = false;
                    }
                }else{
                    $room_list[$key]['is_full'] = false;
                }
            }
        }
        $this->success('获取成功',$room_list);
    }

    //加入房间
    public function join_room(){
        $Gameroom = new Gameroom();
        $pwd = $this->request->post('pwd');
        $room_id = $this->request->post('room_id');

        $room_info = $Gameroom->where('id',$room_id)->find();

        if($room_info){
            if($room_info['pwd'] != md5($pwd)){
                $this->error('密码不对');
            }else{
                if($room_info['join_user_ids']){
                    $join_user_id = explode(",", $room_info['join_user_ids']);
                }else{
                    $join_user_id = [];
                }

                if(count($join_user_id) > 3){
                    $this->error('房间已满员啦');
                }else{
                    $join_user_id[] = $this->auth->id;
                    $effect = $Gameroom->where('id',$room_id)->update(['join_user_ids'=>implode(",", $join_user_id)]);
                    $this->success('加入成功');
                }
            }
        }else{
            $this->error('加入成功',$room_list);
        }
    }
}